/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa036is.validators;

import cz.muni.fi.pa036is.dto.AddressTO;
import cz.muni.fi.pa036is.entity.Address;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 *
 * @author Kuba
 */
public class AddressValidator implements Validator {

    @Override
    public boolean supports(Class<?> type) {
        return Address.class.equals(type);
    }

    @Override
    public void validate(Object o, Errors errors) {
        //ValidationUtils.rejectIfEmpty(errors, "personId", "personId.empty");
        ValidationUtils.rejectIfEmpty(errors, "address.streetAndNumber", "streetAndNumber.empty");
        ValidationUtils.rejectIfEmpty(errors, "address.city", "city.empty");
        ValidationUtils.rejectIfEmpty(errors, "address.zipCode", "zipCode.empty");
        ValidationUtils.rejectIfEmpty(errors, "address.country", "country.empty");

        AddressTO address = (AddressTO) o;

        if (address.getStreetAndNumber().length() > 100) {
            errors.rejectValue("address.streetAndNumber", "streetAndNumber.value");
        }

        if (address.getCity().length() > 100) {
            errors.rejectValue("address.city", "city.value");
        }

        if (address.getZipCode().length() > 10) {
            errors.rejectValue("address.zipCode", "zipCode.value");
        }

        if (address.getCountry().length() > 100) {
            errors.rejectValue("address.country", "country.value");
        }
    }
}
